package cz.muni.fi.pa165.hauntedhouses.web;

import cz.muni.fi.pa165.hauntedhouses.dto.GhostDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.HouseDTO;
import cz.muni.fi.pa165.hauntedhouses.dto.HumanDTO;
import cz.muni.fi.pa165.hauntedhouses.facade.ServiceFacade;
import java.util.ArrayList;
import java.util.List;
import javax.interceptor.Interceptors;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidateNestedProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Q.90
 */
@UrlBinding("/jsp/admin/houses")
public class HousesActionBean implements ActionBean {
 
    final static Logger log = LoggerFactory.getLogger(GhostsActionBean.class);
 
      private ActionBeanContext context;
 
    @SpringBean
    protected  ServiceFacade sf;

    @DefaultHandler
    public Resolution all() {
        log.debug("all()");
       
       return new ForwardResolution("/WEB-INF/jsp/admin/houses.jsp");
    }
    
    public Resolution allHumans() {
        log.debug("allHumans()");
        
        return new ForwardResolution("/WEB-INF/jsp/admin/humans.jsp");
    }
 
    public List<HouseDTO> getHouses() {
        return sf.findAllHouses();
    }
    
    public List<GhostDTO> getGhosts() {
        return sf.findAllGhosts();
    }
    
    public List<HumanDTO> getHumans() {
        return sf.findHumansByHouse(hs);
    }
 
    @ValidateNestedProperties(value = {
            @Validate(on = {"addHuman", "saveHuman"}, field = "name", required = true, minlength=1),
            @Validate(on = {"addHuman", "saveHuman"}, field = "description", required = true, minlength=1),
            @Validate(on = {"addHuman", "saveHuman"}, field = "psyche", required = true, minvalue = 1, maxvalue=12),
            @Validate(on = {"add", "save"}, field = "name", required = true),
            @Validate(on = {"add", "save"}, field = "address", required = true),
            @Validate(on = {"add", "save"}, field = "humanCapacity", required = true, minvalue = 1, maxvalue=12),
            @Validate(on = {"add", "save"}, field = "ghostCapacity", required = true, minvalue = 1, maxvalue=15),
            @Validate(on = {"add", "save"}, field = "history", required = true)
    })
    
    private HumanDTO hum;
 
    public HumanDTO getHuman() {
        return hum;
    }
 
    public void setHuman(HumanDTO human) {
        this.hum = human;
    }
    
    public Resolution addHuman() {
        log.debug("addHuman() human={}", hum);
        hum.setHome(hs);
        sf.createHuman(hum);
        hum = null;
        return new RedirectResolution(this.getClass(),"allHumans").addParameter("house.id", hs.getId());
    }
    
    public Resolution deleteHuman() {
        log.debug("deleteHuman({})", hum.getId());
        sf.removeHuman(hum);
        return new RedirectResolution(this.getClass(),"allHumans").addParameter("house.id", hs.getId());
    }
    
    public Resolution editHumans() {
        log.debug("editHumans() human={}", hs);
        return new ForwardResolution("/WEB-INF/jsp/admin/humans.jsp");
    }
    
    public Resolution editHuman() {
        log.debug("editHuman() human={}", hs);
        return new ForwardResolution("/WEB-INF/jsp/admin/humanEdit.jsp");
    }
    public Resolution saveHuman() {
        log.debug("save() house={}", hum);
        sf.updateHuman(hum);
        return new RedirectResolution(this.getClass(),"allHumans").addParameter("house.id", hs.getId());
    }
    
    public Resolution stornoHuman() {
        log.debug("stornoHuman() human={}", hum);
        return new RedirectResolution(this.getClass(),"allHumans").addParameter("house.id", hs.getId());
    }
    
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"editHuman", "saveHuman", "deleteHuman"})
    public void loadHumanFromDatabase() {
        String ids = context.getRequest().getParameter("human.id");
        if (ids == null) {
            return;
        }
        hum = sf.getHuman(Long.parseLong(ids));
    }
    private HouseDTO hs;
    
    public Resolution add() {
        log.debug("add() house={}", hs);
        hs.setGhosts(new ArrayList<GhostDTO>());
        hs.setHumans(new ArrayList<HumanDTO>());
        sf.createHouse(hs);
        return new RedirectResolution(this.getClass(), "all");
    }
 
    public HouseDTO getHouse() {
        return hs;
    }
 
    public void setHouse(HouseDTO house) {
        this.hs = house;
    }
    
     public Resolution delete() {
        log.debug("delete({})", hs.getId());
        List<HumanDTO> listHum=hs.getHumans();
        List<GhostDTO> listGho=hs.getGhosts();
        
        for(int i=0;i<listHum.size();i++){
            sf.removeHumanFromHouse(hs,listHum.get(i));
        }
        sf.removeHouse(hs);
        return new RedirectResolution(this.getClass(), "all");
    }
     
    @Before(stages = LifecycleStage.BindingAndValidation, on = {"allHumans","edit", "save", "delete","addHuman","editHumans","saveHuman","stornoHuman","deleteHuman"})
    public void loadHouseFromDatabase() {
        String ids = context.getRequest().getParameter("house.id");
        if (ids == null) {
            return;
        }
        hs = sf.getHouse(Long.parseLong(ids));
    }
     
    public Resolution edit() {
        log.debug("edit() house={}", hs);
        return new ForwardResolution("/WEB-INF/jsp/admin/houseEdit.jsp");
    }
 
    public Resolution save() {
        log.debug("save() house={}", hs);
        sf.updateHouse(hs);
        return new RedirectResolution(this.getClass(), "all");
    }
    
    public Resolution storno() {
        log.debug("storno() house={}", hs);
        return new RedirectResolution(this.getClass(), "all");
    }

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }
 
    @Override
    public ActionBeanContext getContext() {
        return context;
    }
}

